# Create your views here.
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.contrib.auth.models import User
from cartoon_us.clan.models import clan
from cartoon_us.gallery.models import ArtWork,WrittenWork,Comic
from cartoon_us.people.models import searchResult,lastsearch

def show(request,kind,page):
    if request.user.is_authenticated():
        page=int(page)
        data={}
        u=[]
        if kind.find('-')!=-1:
            user=kind[0:kind.find('-')]
            kind=kind[kind.find('-')+1:]
            of=User.objects.get(username=user)
        if kind == 'search':
            u=[]
            if (request.method=='POST'):
                term = request.POST['searchTerm']
                ls=lastsearch.objects.get(id=1)
                ls.term=term
                ls.save()
            else:
                term=lastsearch.objects.get(id=1).term
            for x in User.objects.all():
                if x.username.find(term)!=-1:
                    s=searchResult(name = x.username, title = x.username,ctype = 'profile')
                    u.append(s)
            for x in ArtWork.objects.all():
                if x.title.find(term)!=-1:
                    s=searchResult(name=x.of.username,title=x.title,ctype='illustration')
                    u.append(s)
            for x in WrittenWork.objects.all():
                if x.title.find(term)!=-1:
                    s=searchResult(name=x.of.username,title=x.title,ctype='fiction')
                    u.append(s)
            for x in Comic.objects.all():
                if x.title.find(term)!=-1:
                    s=searchResult(name=x.of.username,title=x.title,ctype='comics')
                    u.append(s)
            data['search']=True
        if kind == 'people':
            u=User.objects.all()
            data['people']=True
        if kind == 'clan':
            u=clan.objects.all()
            data['clan']=True
        if kind == 'friends':
            u=[]
            tu=of.friend_set.all()
            for i in range(len(tu)):
                u.append(User.objects.get(username=tu[i].user))           
            data['people']=True
        if kind == 'fiction':
            u=WrittenWork.objects.all()
            for uu in u:
                uu.abstract=uu.abstract[0:150]
            data['fiction']=True
        if kind == 'myfiction':
            u=[]
            for f in WrittenWork.objects.all():
                if f.of.username==user:
                    f.abstract=f.abstract[0:150]
                    u.append(f)
            data['fiction']=True
        if kind == 'gallery':
            u=ArtWork.objects.all()
            data['illustrations']=True
        if kind == 'comics':
            u=Comic.objects.all()
            data['comics']=True
        if kind == 'mycomics':
            u=[]
            for f in Comic.objects.all():
                if f.of.username==user:
                    u.append(f)
            data['comics']=True
        if kind == 'illustrations': 
            u=[]
            for f in ArtWork.objects.all():
                if f.of.username==user:
                    u.append(f)
            data['illustrations']=True
        if kind == 'clans':
            u=[]
            tu=of.my_clans_set.all()
            for i in range(len(tu)):
                u.append(clan.objects.get(clanName=tu[i].clan))
            data['clan']=True
        
        if len(u)>page*10:
            data['next']=True
        else:
            data['next']=False
        if page == 1:
            data['prev']=False
        else:
            data['prev']=True
        l=[]
        r=[]
        for i in range(5 if len(u)>5+(page-1)*10 else len(u)-(page-1)*10):
            l.append(u[i+10*(page-1)])
        for i in range(5 if len(u)>10+(page-1)*10 else len(u)-5-(page-1)*10):
            r.append(u[i+5+10*(page-1)])
        data['left']=l
        data['right']=r
        data['username']=request.user.username
        data['npage']=page+1
        data['lpage']=page-1
        return render_to_response('backend/list_page.html',data)
    else:
        return HttpResponseRedirect('../../home')

def nopage(request,kind):
    return HttpResponseRedirect('../list/'+kind+'/1')


            

